home *** CD-ROM | disk | FTP | other *** search
/ Clickx 63 / Clickx 63.iso / software / multimedia / mirov204 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / config.js < prev    next >
Encoding:
Text File  |  2008-02-08  |  17.8 KB  |  583 lines

  1. //@line 41 "/e/xr19rel/WINNT_5.2_Depend/mozilla/toolkit/components/viewconfig/content/config.js"
  2.  
  3. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  4. const nsISupportsString = Components.interfaces.nsISupportsString;
  5. const nsIPromptService = Components.interfaces.nsIPromptService;
  6. const nsIPrefService = Components.interfaces.nsIPrefService;
  7. const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
  8. const nsIClipboardHelper = Components.interfaces.nsIClipboardHelper;
  9. const nsIAtomService = Components.interfaces.nsIAtomService;
  10.  
  11. const nsSupportsString_CONTRACTID = "@mozilla.org/supports-string;1";
  12. const nsPrompt_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  13. const nsPrefService_CONTRACTID = "@mozilla.org/preferences-service;1";
  14. const nsClipboardHelper_CONTRACTID = "@mozilla.org/widget/clipboardhelper;1";
  15. const nsAtomService_CONTRACTID = "@mozilla.org/atom-service;1";
  16.  
  17. const gPromptService = Components.classes[nsPrompt_CONTRACTID].getService(nsIPromptService);
  18. const gPrefService = Components.classes[nsPrefService_CONTRACTID].getService(nsIPrefService);
  19. const gPrefBranch = gPrefService.getBranch(null).QueryInterface(Components.interfaces.nsIPrefBranch2);
  20. const gClipboardHelper = Components.classes[nsClipboardHelper_CONTRACTID].getService(nsIClipboardHelper);
  21. const gAtomService = Components.classes[nsAtomService_CONTRACTID].getService(nsIAtomService);
  22.  
  23. var gLockAtoms = [gAtomService.getAtom("default"), gAtomService.getAtom("user"), gAtomService.getAtom("locked")];
  24. // we get these from a string bundle
  25. var gLockStrs = [];
  26. var gTypeStrs = [];
  27.  
  28. const PREF_IS_DEFAULT_VALUE = 0;
  29. const PREF_IS_USER_SET = 1;
  30. const PREF_IS_LOCKED = 2;
  31.  
  32. var gPrefHash = {};
  33. var gPrefArray = [];
  34. var gPrefView = gPrefArray; // share the JS array
  35. var gFastIndex = 0;
  36. var gSortedColumn = "prefCol";
  37. var gSortFunction = null;
  38. var gSortDirection = 1; // 1 is ascending; -1 is descending
  39. var gConfigBundle = null;
  40.  
  41. var view = {
  42.   get rowCount() { return gPrefView.length; },
  43.   getCellText : function(index, col) {
  44.     if (!(index in gPrefView))
  45.       return "";
  46.     
  47.     var value = gPrefView[index][col.id];
  48.  
  49.     switch (col.id) {
  50.       case "lockCol":           
  51.         return gLockStrs[value];
  52.       case "typeCol":
  53.         return gTypeStrs[value];
  54.       default:
  55.         return value;
  56.     }
  57.   },
  58.   getRowProperties : function(index, prop) {},
  59.   getCellProperties : function(index, col, prop) {
  60.     if (index in gPrefView)
  61.       prop.AppendElement(gLockAtoms[gPrefView[index].lockCol]);
  62.   },
  63.   getColumnProperties : function(col, prop) {},
  64.   treebox : null,
  65.   selection : null,
  66.   isContainer : function(index) { return false; },
  67.   isContainerOpen : function(index) { return false; },
  68.   isContainerEmpty : function(index) { return false; },
  69.   isSorted : function() { return true; },
  70.   canDrop : function(index, orientation) { return false; },
  71.   drop : function(row, orientation) {},
  72.   setTree : function(out) { this.treebox = out; },
  73.   getParentIndex: function(rowIndex) { return -1; },
  74.   hasNextSibling: function(rowIndex, afterIndex) { return false; },
  75.   getLevel: function(index) { return 1; },
  76.   getImageSrc: function(row, col) { return ""; },
  77.   toggleOpenState : function(index) {},
  78.   cycleHeader: function(col) {
  79.     var index = this.selection.currentIndex;
  80.     if (col.id == gSortedColumn)
  81.       gSortDirection = -gSortDirection;
  82.     if (col.id == gSortedColumn && gFastIndex == gPrefArray.length) {
  83.       gPrefArray.reverse();
  84.       if (gPrefView != gPrefArray)
  85.         gPrefView.reverse();
  86.       if (index >= 0)
  87.         index = gPrefView.length - index - 1;
  88.     }
  89.     else {
  90.       var pref = null;
  91.       if (index >= 0) {
  92.         if (gPrefArray != gPrefView)
  93.           index = gPrefView.length - index - 1;
  94.         else
  95.           pref = gPrefArray[index];
  96.       }
  97.       var old = document.getElementById(gSortedColumn);
  98.       old.setAttribute("sortDirection", "");
  99.       gPrefArray.sort(gSortFunction = gSortFunctions[col.id]);
  100.       if (gPrefView != gPrefArray) {
  101.         if (col.id == gSortedColumn)
  102.           gPrefView.reverse();
  103.         else
  104.           gPrefView.sort(gSortFunction);
  105.       }
  106.       gSortedColumn = col.id;
  107.       if (pref)
  108.         index = getIndexOfPref(pref);
  109.     }
  110.     col.element.setAttribute("sortDirection", gSortDirection > 0 ? "ascending" : "descending");
  111.     this.treebox.invalidate();
  112.     if (index >= 0) {
  113.       this.selection.select(index);
  114.       this.treebox.ensureRowIsVisible(index);
  115.     }
  116.     gFastIndex = gPrefArray.length;
  117.   },
  118.   selectionChanged : function() {},
  119.   cycleCell: function(row, col) {},
  120.   isEditable: function(row, col) {return false; },
  121.   isSelectable: function(row, col) {return false; },
  122.   setCellValue: function(row, col, value) {},
  123.   setCellText: function(row, col, value) {},
  124.   performAction: function(action) {},
  125.   performActionOnRow: function(action, row) {},
  126.   performActionOnCell: function(action, row, col) {},
  127.   isSeparator: function(index) {return false; }
  128. };
  129.  
  130. // find the index in gPrefView of a pref object
  131. // or -1 if it does not exist in the filtered view
  132. function getViewIndexOfPref(pref)
  133. {
  134.   var low = -1, high = gPrefView.length;
  135.   var index = (low + high) >> 1;
  136.   while (index > low) {
  137.     var mid = gPrefView[index];
  138.     if (mid == pref)
  139.       return index;
  140.     if (gSortFunction(mid, pref) < 0)
  141.       low = index;
  142.     else
  143.       high = index;
  144.     index = (low + high) >> 1;
  145.   }
  146.   return -1;
  147. }
  148.  
  149. // find the index in gPrefArray of a pref object
  150. // either one that was looked up in gPrefHash
  151. // or in case it was moved after sorting
  152. function getIndexOfPref(pref)
  153. {
  154.   var low = -1, high = gFastIndex;
  155.   var index = (low + high) >> 1;
  156.   while (index > low) {
  157.     var mid = gPrefArray[index];
  158.     if (mid == pref)
  159.       return index;
  160.     if (gSortFunction(mid, pref) < 0)
  161.       low = index;
  162.     else
  163.       high = index;
  164.     index = (low + high) >> 1;
  165.   }
  166.  
  167.   for (index = gFastIndex; index < gPrefArray.length; ++index)
  168.     if (gPrefArray[index] == pref)
  169.       break;
  170.   return index;
  171. }
  172.  
  173. function getNearestIndexOfPref(pref)
  174. {
  175.   var low = -1, high = gFastIndex;
  176.   var index = (low + high) >> 1;
  177.   while (index > low) {
  178.     if (gSortFunction(gPrefArray[index], pref) < 0)
  179.       low = index;
  180.     else
  181.       high = index;
  182.     index = (low + high) >> 1;
  183.   }
  184.   return high;
  185. }
  186.  
  187. var gPrefListener =
  188. {
  189.   observe: function(subject, topic, prefName)
  190.   {
  191.     if (topic != "nsPref:changed")
  192.       return;
  193.  
  194.     if (/^capability\./.test(prefName)) // avoid displaying "private" preferences
  195.       return;
  196.  
  197.     var index = gPrefArray.length;
  198.     if (prefName in gPrefHash) {
  199.       index = getViewIndexOfPref(gPrefHash[prefName]);
  200.       fetchPref(prefName, getIndexOfPref(gPrefHash[prefName]));
  201.       if (index >= 0) {
  202.         // Might need to update the filtered view
  203.         gPrefView[index] = gPrefHash[prefName];
  204.         view.treebox.invalidateRow(index);
  205.       }
  206.       if (gSortedColumn == "lockCol" || gSortedColumn == "valueCol")
  207.         gFastIndex = 1; // TODO: reinsert and invalidate range
  208.     } else {
  209.       fetchPref(prefName, index);
  210.       if (index == gFastIndex) {
  211.         // Keep the array sorted by reinserting the pref object
  212.         var pref = gPrefArray.pop();
  213.         index = getNearestIndexOfPref(pref);
  214.         gPrefArray.splice(index, 0, pref);
  215.         gFastIndex = gPrefArray.length;
  216.       }
  217.       if (gPrefView == gPrefArray)
  218.         view.treebox.rowCountChanged(index, 1);
  219.     }
  220.   }
  221. };
  222.  
  223. function prefObject(prefName, prefIndex)
  224. {
  225.   this.prefCol = prefName;
  226. }
  227.  
  228. prefObject.prototype =
  229. {
  230.   lockCol: PREF_IS_DEFAULT_VALUE,
  231.   typeCol: nsIPrefBranch.PREF_STRING,
  232.   valueCol: ""
  233. };
  234.  
  235. function fetchPref(prefName, prefIndex)
  236. {
  237.   var pref = new prefObject(prefName);
  238.  
  239.   gPrefHash[prefName] = pref;
  240.   gPrefArray[prefIndex] = pref;
  241.  
  242.   if (gPrefBranch.prefIsLocked(prefName))
  243.     pref.lockCol = PREF_IS_LOCKED;
  244.   else if (gPrefBranch.prefHasUserValue(prefName))
  245.     pref.lockCol = PREF_IS_USER_SET;
  246.  
  247.   try {
  248.     switch (gPrefBranch.getPrefType(prefName)) {
  249.       case gPrefBranch.PREF_BOOL:
  250.         pref.typeCol = gPrefBranch.PREF_BOOL;
  251.         // convert to a string
  252.         pref.valueCol = gPrefBranch.getBoolPref(prefName).toString();
  253.         break;
  254.       case gPrefBranch.PREF_INT:
  255.         pref.typeCol = gPrefBranch.PREF_INT;
  256.         // convert to a string
  257.         pref.valueCol = gPrefBranch.getIntPref(prefName).toString();
  258.         break;
  259.       default:
  260.       case gPrefBranch.PREF_STRING:
  261.         pref.valueCol = gPrefBranch.getComplexValue(prefName, nsISupportsString).data;
  262.         // Try in case it's a localized string (will throw an exception if not)
  263.         if (pref.lockCol == PREF_IS_DEFAULT_VALUE &&
  264.             /^chrome:\/\/.+\/locale\/.+\.properties/.test(pref.valueCol))
  265.           pref.valueCol = gPrefBranch.getComplexValue(prefName, nsIPrefLocalizedString).data;
  266.         break;
  267.     }
  268.   } catch (e) {
  269.     // Also catch obscure cases in which you can't tell in advance
  270.     // that the pref exists but has no user or default value...
  271.   }
  272. }
  273.  
  274. function onConfigLoad()
  275. {
  276.   // Load strings
  277.   gConfigBundle = document.getElementById("configBundle");
  278.  
  279.   gLockStrs[PREF_IS_DEFAULT_VALUE] = gConfigBundle.getString("default");
  280.   gLockStrs[PREF_IS_USER_SET] = gConfigBundle.getString("user");
  281.   gLockStrs[PREF_IS_LOCKED] = gConfigBundle.getString("locked");
  282.  
  283.   gTypeStrs[nsIPrefBranch.PREF_STRING] = gConfigBundle.getString("string");
  284.   gTypeStrs[nsIPrefBranch.PREF_INT] = gConfigBundle.getString("int");
  285.   gTypeStrs[nsIPrefBranch.PREF_BOOL] = gConfigBundle.getString("bool");
  286.  
  287.   var showWarning = gPrefBranch.getBoolPref("general.warnOnAboutConfig");
  288.  
  289.   if (showWarning)
  290.     document.getElementById("warningButton").focus();
  291.   else
  292.     ShowPrefs();
  293. }
  294.  
  295. // Unhide the warning message
  296. function ShowPrefs()
  297. {
  298.   var prefCount = { value: 0 };
  299.   var prefArray = gPrefBranch.getChildList("", prefCount);
  300.  
  301.   for (var i = 0; i < prefCount.value; ++i) 
  302.   {
  303.     var prefName = prefArray[i];
  304.     if (/^capability\./.test(prefName)) // avoid displaying "private" preferences
  305.       continue;
  306.  
  307.     fetchPref(prefName, gPrefArray.length);
  308.   }
  309.  
  310.   var descending = document.getElementsByAttribute("sortDirection", "descending");
  311.   if (descending.item(0)) {
  312.     gSortedColumn = descending[0].id;
  313.     gSortDirection = -1;
  314.   }
  315.   else {
  316.     var ascending = document.getElementsByAttribute("sortDirection", "ascending");
  317.     if (ascending.item(0))
  318.       gSortedColumn = ascending[0].id;
  319.     else
  320.       document.getElementById(gSortedColumn).setAttribute("sortDirection", "ascending");
  321.   }
  322.   gSortFunction = gSortFunctions[gSortedColumn];
  323.   gPrefArray.sort(gSortFunction);
  324.   gFastIndex = gPrefArray.length;
  325.   
  326.   gPrefBranch.addObserver("", gPrefListener, false);
  327.  
  328.   var configTree = document.getElementById("configTree");
  329.   configTree.view = view;
  330.   configTree.controllers.insertControllerAt(0, configController);
  331.  
  332.   document.getElementById("configDeck").setAttribute("selectedIndex", 1);
  333.   if (!document.getElementById("showWarningNextTime").checked)
  334.     gPrefBranch.setBoolPref("general.warnOnAboutConfig", false);
  335.  
  336.   document.getElementById("textbox").focus();
  337. }
  338.  
  339. function onConfigUnload()
  340. {
  341.   if (document.getElementById("configDeck").getAttribute("selectedIndex") == 1) {
  342.     gPrefBranch.removeObserver("", gPrefListener);
  343.     var configTree = document.getElementById("configTree");
  344.     configTree.view = null;
  345.     configTree.controllers.removeController(configController);
  346.   }
  347. }
  348.  
  349. function FilterPrefs()
  350. {
  351.   var substring = document.getElementById("textbox").value;
  352.   var rex;
  353.   // Check for "/regex/[i]"
  354.   if (substring.charAt(0) == '/') {
  355.     var r = substring.match(/^\/(.*)\/(i?)$/);
  356.     try {
  357.       rex = RegExp(r[1], r[2]);
  358.     }
  359.     catch (e) {
  360.       return; // Do nothing on incomplete or bad RegExp
  361.     }
  362.   }
  363.  
  364.   var prefCol = view.selection.currentIndex < 0 ? null : gPrefView[view.selection.currentIndex].prefCol;
  365.   var oldlen = gPrefView.length;
  366.   gPrefView = gPrefArray;
  367.   if (substring) {
  368.     gPrefView = [];
  369.     if (!rex)
  370.       rex = RegExp(substring.replace(/([^* \w])/g, "\\$1").replace(/^\*+/, "")
  371.                             .replace(/\*+/g, ".*"), "i");
  372.     for (var i = 0; i < gPrefArray.length; ++i)
  373.       if (rex.test(gPrefArray[i].prefCol + ";" + gPrefArray[i].valueCol))
  374.         gPrefView.push(gPrefArray[i]);
  375.     if (gFastIndex < gPrefArray.length)
  376.       gPrefView.sort(gSortFunction);
  377.   }
  378.   view.treebox.invalidate();
  379.   view.treebox.rowCountChanged(oldlen, gPrefView.length - oldlen);
  380.   gotoPref(prefCol);
  381.   document.getElementById("button").disabled = !substring;
  382. }
  383.  
  384. function ClearFilter(button)
  385. {
  386.   var textbox = document.getElementById("textbox");
  387.   textbox.value = "";
  388.   textbox.focus();
  389.   FilterPrefs();
  390. }
  391.  
  392. function prefColSortFunction(x, y)
  393. {
  394.   if (x.prefCol > y.prefCol)
  395.     return gSortDirection;
  396.   if (x.prefCol < y.prefCol) 
  397.     return -gSortDirection;
  398.   return 0;
  399. }
  400.  
  401. function lockColSortFunction(x, y)
  402. {
  403.   if (x.lockCol != y.lockCol)
  404.     return gSortDirection * (y.lockCol - x.lockCol);
  405.   return prefColSortFunction(x, y);
  406. }
  407.  
  408. function typeColSortFunction(x, y)
  409. {
  410.   if (x.typeCol != y.typeCol) 
  411.     return gSortDirection * (y.typeCol - x.typeCol);
  412.   return prefColSortFunction(x, y);
  413. }
  414.  
  415. function valueColSortFunction(x, y)
  416. {
  417.   if (x.valueCol > y.valueCol)
  418.     return gSortDirection;
  419.   if (x.valueCol < y.valueCol) 
  420.     return -gSortDirection;
  421.   return prefColSortFunction(x, y);
  422. }
  423.  
  424. const gSortFunctions =
  425. {
  426.   prefCol: prefColSortFunction, 
  427.   lockCol: lockColSortFunction, 
  428.   typeCol: typeColSortFunction, 
  429.   valueCol: valueColSortFunction
  430. };
  431.  
  432. const configController = {
  433.   supportsCommand: function supportsCommand(command) {
  434.     return command == "cmd_copy";
  435.   },
  436.   isCommandEnabled: function isCommandEnabled(command) {
  437.     return view.selection && view.selection.currentIndex >= 0;
  438.   },
  439.   doCommand: function doCommand(command) {
  440.     copyPref();
  441.   },
  442.   onEvent: function onEvent(event) {
  443.   }
  444. }
  445.  
  446. function updateContextMenu()
  447. {
  448.   var lockCol = PREF_IS_LOCKED;
  449.   var typeCol = nsIPrefBranch.PREF_STRING;
  450.   var valueCol = "";
  451.   var copyDisabled = true;
  452.   var prefSelected = view.selection.currentIndex >= 0;
  453.  
  454.   if (prefSelected) {
  455.     var prefRow = gPrefView[view.selection.currentIndex];
  456.     lockCol = prefRow.lockCol;
  457.     typeCol = prefRow.typeCol;
  458.     valueCol = prefRow.valueCol;
  459.     copyDisabled = false;
  460.   }
  461.  
  462.   var copyPref = document.getElementById("copyPref");
  463.   copyPref.setAttribute("disabled", copyDisabled);
  464.  
  465.   var copyName = document.getElementById("copyName");
  466.   copyName.setAttribute("disabled", copyDisabled);
  467.  
  468.   var copyValue = document.getElementById("copyValue");
  469.   copyValue.setAttribute("disabled", copyDisabled);
  470.  
  471.   var resetSelected = document.getElementById("resetSelected");
  472.   resetSelected.setAttribute("disabled", lockCol != PREF_IS_USER_SET);
  473.  
  474.   var canToggle = typeCol == nsIPrefBranch.PREF_BOOL && valueCol != "";
  475.   // indicates that a pref is locked or no pref is selected at all
  476.   var isLocked = lockCol == PREF_IS_LOCKED;
  477.  
  478.   var modifySelected = document.getElementById("modifySelected");
  479.   modifySelected.setAttribute("disabled", isLocked);
  480.   modifySelected.hidden = canToggle;
  481.  
  482.   var toggleSelected = document.getElementById("toggleSelected");
  483.   toggleSelected.setAttribute("disabled", isLocked);
  484.   toggleSelected.hidden = !canToggle;
  485. }
  486.  
  487. function copyPref()
  488. {
  489.   var pref = gPrefView[view.selection.currentIndex];
  490.   gClipboardHelper.copyString(pref.prefCol + ';' + pref.valueCol);
  491. }
  492.  
  493. function copyName()
  494. {
  495.   gClipboardHelper.copyString(gPrefView[view.selection.currentIndex].prefCol);
  496. }
  497.  
  498. function copyValue()
  499. {
  500.   gClipboardHelper.copyString(gPrefView[view.selection.currentIndex].valueCol);
  501. }
  502.  
  503. function ModifySelected()
  504. {
  505.   if (view.selection.currentIndex >= 0)
  506.     ModifyPref(gPrefView[view.selection.currentIndex]);
  507. }
  508.  
  509. function ResetSelected()
  510. {
  511.   var entry = gPrefView[view.selection.currentIndex];
  512.   gPrefBranch.clearUserPref(entry.prefCol);
  513. }
  514.  
  515. function NewPref(type)
  516. {
  517.   var result = { value: "" };
  518.   var dummy = { value: 0 };
  519.   if (gPromptService.prompt(window,
  520.                             gConfigBundle.getFormattedString("new_title", [gTypeStrs[type]]),
  521.                             gConfigBundle.getString("new_prompt"),
  522.                             result,
  523.                             null,
  524.                             dummy) && result.value) {
  525.     var pref;
  526.     if (result.value in gPrefHash)
  527.       pref = gPrefHash[result.value];
  528.     else
  529.       pref = { prefCol: result.value, lockCol: PREF_IS_DEFAULT_VALUE, typeCol: type, valueCol: "" };
  530.     if (ModifyPref(pref))
  531.       setTimeout(gotoPref, 0, result.value);
  532.   }
  533. }
  534.  
  535. function gotoPref(pref)
  536. {
  537.   // make sure the pref exists and is displayed in the current view
  538.   var index = pref in gPrefHash ? getViewIndexOfPref(gPrefHash[pref]) : -1;
  539.   if (index >= 0) {
  540.     view.selection.select(index);
  541.     view.treebox.ensureRowIsVisible(index);
  542.   } else {
  543.     view.selection.clearSelection();
  544.     view.selection.currentIndex = -1;
  545.   }
  546. }
  547.  
  548. function ModifyPref(entry)
  549. {
  550.   if (entry.lockCol == PREF_IS_LOCKED)
  551.     return false;
  552.   var title = gConfigBundle.getFormattedString("modify_title", [gTypeStrs[entry.typeCol]]);
  553.   if (entry.typeCol == nsIPrefBranch.PREF_BOOL) {
  554.     var check = { value: entry.valueCol == "false" };
  555.     if (!entry.valueCol && !gPromptService.select(window, title, entry.prefCol, 2, [false, true], check))
  556.       return false;
  557.     gPrefBranch.setBoolPref(entry.prefCol, check.value);
  558.   }
  559.   else if (entry.typeCol == nsIPrefBranch.PREF_INT) {
  560.     var params = { windowTitle: title,
  561.                    label: entry.prefCol,
  562.                    value: entry.valueCol,
  563.                    cancelled: true };
  564.     window.openDialog("chrome://global/content/configIntValue.xul", "_blank",
  565.                       "chrome,titlebar,centerscreen,modal", params);
  566.     if (params.cancelled)
  567.       return false;
  568.     gPrefBranch.setIntPref(entry.prefCol, params.value);
  569.   }
  570.   else {
  571.     var result = { value: entry.valueCol };
  572.     var dummy = { value: 0 };
  573.     if (!gPromptService.prompt(window, title, entry.prefCol, result, null, dummy))
  574.       return false;
  575.     var supportsString = Components.classes[nsSupportsString_CONTRACTID].createInstance(nsISupportsString);
  576.     supportsString.data = result.value;
  577.     gPrefBranch.setComplexValue(entry.prefCol, nsISupportsString, supportsString);
  578.   }
  579.  
  580.   gPrefService.savePrefFile(null);
  581.   return true;
  582. }
  583.